const Service = require('./index')
const loginLogModel = require('@/models/login_log.model')

class LoginLogService extends Service {
  /**
   * 登录日志分页查询
   * @param {*} pageNo
   * @param {*} pageSize
   * @param {*} searchKeywords
   * @param {*} ip
   * @param {*} loginType
   * @param {*} startTime
   * @param {*} endTime
   */
  async findPage (pageNo = 1, pageSize = 20, {
    searchKeywords,
    ip,
    loginType,
    startTime,
    endTime
  }) {
    try {
      const { list, total } = await loginLogModel.findPage({
        pageNo,
        pageSize,
        andConditions: {
          equal: {
            'log.loginType': loginType
          },
          like: {
            'log.ip': ip
          }
        },
        orConditions: {
          like: {
            'user.nickname': searchKeywords,
            'user.username': searchKeywords
          }
        },
        betweenConditions: {
          'log.loginTime': [startTime, endTime]
        }
      })
      return this.skipPage(list, total, pageNo, pageSize)
    } catch (error) {
      throw new Error(error)
    }
  }

  async batchDelete (ids) {
    if (!ids?.length) {
      return {
        errorMessage: 'ids不能为空'
      }
    }
    try {
      await loginLogModel.delete(ids)
    } catch (error) {
      throw new Error(error)
    }
  }
}

module.exports = new LoginLogService()
